import {ElMessage as Message, ElMessageBox as MessageBox} from 'element-plus'
import {isBoolean, isEmpty} from './helpers'
import {MessageType} from "element-plus/es/el-message/src/types";

export function Toast(message: string | Error, type: MessageType = 'success', duration: number = 3 * 1000) {
    if (isEmpty(message)) {
        return
    }
    if (isBoolean(type)) {
        type = type ? 'success' : 'error'
    }
    if (message instanceof Error) {
        message = message.message
    }
    Message({
        message,
        type,
        duration
    })
}

export const ToastBox = {
    success(message: string, duration?: number) {
        Toast(message, 'success', duration)
    },
    warning(message: string, duration?: number) {
        Toast(message, 'warning', duration)
    },
    danger(message: string, duration?: number) {
        Toast(message, 'error', duration)
    },
    error(message: string, duration?: number) {
        Toast(message, 'error', duration)
    },
    info(message: string, duration?: number) {
        Toast(message, 'info', duration)
    },
    async confirm(message = '确认操作？', title = '提示', options = {}) {
        message = message || '确认操作？'
        return 'confirm' === await MessageBox.confirm(message, title, options).catch(_ => _)
    }
}
